package com.kaili.basic.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.MdmApplicationInstalledQueryDto;
import com.kaili.basic.pojo.entity.MdmApplicationInstallExport;
import com.kaili.basic.pojo.entity.MdmApplicationInstalled;
import com.kaili.basic.pojo.vo.MdmApplicationInstallVo;
import com.kaili.basic.service.IApplicationInstallService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.util.ExcelUtil;
import com.kaili.common.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 *@Desc:被安装应用控制层
 *@author:chenmengming
 *@Time: 19-7-25 3:26
 *@Copyright: @ 2019 杭州凯立通信有限公司 版权所有
 *@Warning 本内容仅限于公司内部传阅，禁止外泄或用于其他商业目的
 */

@RestController
@ApiVersion(1)
@RequestMapping("/{api_version}")
@Api(tags = "被安装应用", description = "被安装应用相关API")
public class MdmApplicationInstallController {
    /**
     *  mdm被安装应用service对象
     */
    @Autowired
    IApplicationInstallService applicationInstallService;

//    Logger log = LoggerFactory.getLogger(MdmEmployeeController.class);

    /**
     * @Desc:  条件查询获取mdm被安装应用信息
     * @author: chenmengming
     * @Time: 2019/7/26 10:04
     * @param applicationInstalledQueryDto
     * @return com.baomidou.mybatisplus.plugins.Page<com.kaili.basic.pojo.vo.MdmApplicationInstallVo>
     */
    @GetMapping(value = "/mdmApplicationInstall/queryApplication")
    @ApiOperation(value = "条件查询被安装应用信息",notes = "条件查询被安装应用信息")
    public Page<MdmApplicationInstallVo> queryApplication(@ModelAttribute MdmApplicationInstalledQueryDto applicationInstalledQueryDto){
        MdmApplicationInstalled application=new MdmApplicationInstalled();
        BeanUtils.copyProperties(applicationInstalledQueryDto,application);
        return applicationInstallService.getMdmApplicationByKeywords(new Page<>(applicationInstalledQueryDto.getPage(),applicationInstalledQueryDto.getPageSize()),applicationInstalledQueryDto);
    }

    /**
     * @Desc:  mdm被安装应用导出
     * @author: chenmengming
     * @Time: 2019/7/26 10:04
     * @param response
     * @param applicationQueryDto
     * @return void
     */
    @GetMapping(value = "/mdmApplicationInstall/applicationExport")
    @ApiOperation(value = "导出应用被安装",notes = "导出应用被安装")
    public void ApplicationExport(@ModelAttribute MdmApplicationInstalledQueryDto applicationQueryDto, HttpServletResponse response){
        System.out.println("~~~~~~~~~开始导出了~~~~~~~~~~");
//    MdmApplicationInstalled application=new MdmApplicationInstalled();
        /*application.setPage(applicationQueryDto.getPage());
        application.setPageSize(applicationQueryDto.getPageSize());
        application.setAppName(applicationQueryDto.getAppName());
        application.setIsInstall(applicationQueryDto.getIsInstall());
        application.setModifyTime(applicationQueryDto.getModifyTime());*/
        applicationQueryDto.setPageSize(Integer.MAX_VALUE);
        try {
            ExcelUtil.exportExcel(applicationInstallService.getApplicationxport(applicationQueryDto),"被安装应用","被安装应用", MdmApplicationInstallExport.class,"被安装应用"+ UUIDUtil.getUUId()+".xls",response);
        } catch (RestException e) {
//            log.error("导出失败:"+e.getMessage());
        }
    }
}
